<extend name="Layout/ins_page"/>
<block name="content">
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <?php if (!session_id()) session_start();date_default_timezone_set('PRC'); ?>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>授权列表</title>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
        <link href="/sap/demo.css" rel="stylesheet" type="text/css"/>
        <script src="/sap/scripts/boot.js" type="text/javascript"></script>
        <style>

        </style>

    </head>
    <body>

    <fieldset id="fd1" style="">
        <br/>
        <div id='fm1'>
            选择供应商:
            <if condition=' ($supplierID) '>
                <a target="_blank" href="/SapSupplier/supplieredit/id/<?php echo $supplierID;?>"><?php echo $supplierName;?></a>
                <input type="hidden" style="visibility: hidden" class="mini-buttonedit" id="spl-select" text="<?php echo $supplierName;?>" value="<?php echo $supplierID;?>">
                <else />
                <input id="spl-select" class="mini-buttonedit" onbuttonclick="onButtonEdit2" value="" text="" name=""
                       textName="" style="width: 250px"/>
            </if>

            <br/>
            <br/>
        </div>
    </fieldset>


    <div id="datagrid1" class="mini-datagrid" style="height:200px;width: 100%;"
         url="/SapSupplier/getauthData" idField="id"
         allowResize="true"
         allowCellEdit="true" allowCellSelect="true" multiSelect="true"
         allowCellValid="true" oncellvalidation=""
    >

        <div property="columns">
            <div field="examine_status" headerAlign="center">审核状态</div>
            <div field="code" headerAlign="center">平台代码</div>
            <div field="cname" headerAlign="center">平台名称</div>
            <div field="category" headerAlign="center">平台类目</div>
            <div field="brand_name" headerAlign="center">品牌</div>
            <div field="level" headerAlign="center">授权级别</div>
            <div field="auth_chain" headerAlign="center">授权链</div>
            <div field="status" headerAlign="center">状态</div>
            <div field="start" headerAlign="center">授权开始时间
                <input property="editor" class="mini-datepicker" valueType='string' style="width:100%;"
                       format="yyyy-MM-dd H:mm:ss" timeFormat="H:mm:ss" showTime="true"/>
            </div>
            <div field="end" headerAlign="center">授权结束时间
                <input property="editor" class="mini-datepicker" valueType='string' style="width:100%;"
                       format="yyyy-MM-dd H:mm:ss" timeFormat="H:mm:ss" showTime="true"/>
            </div>
            <div field="ctime" headerAlign="center">添加时间</div>
            <div field="note" headerAlign="center">备注</div>
            <div name="action" width="100" headerAlign="center">#</div>
        </div>
    </div>

    <br/>
    <div id="grid2" class="mini-datagrid" style="width: 100%; height: 200px;" allowResize="true" allowCellEdit="true"
         idField="id" multiSelect="true" allowCellEdit="true" allowCellSelect="true" onCellendedit='cellendedit'
         url="/SapSupplier/getSapSupplierProfitListData"
    >
        <div property="columns">
            <div field="examine_status" headerAlign="center">审核状态</div>
            <div header="物料编号" field="sap_code"></div>
            <div header="物料名称" field="item_name"></div>
            <div header="颜色*" field="color"><input property="editor" class="mini-textbox" style="width:100%;"/></div>
            <div header="图片" field="logo"></div>
            <div header="订货说明*" field="caption"><input property="editor" class="mini-textbox" style="width:100%;"/></div>
            <div header="毛利" field="maoli"></div>
            <div header="商城价*" field="mall_price"><input property="editor" class="mini-spinner" minValue="0"
                                                        maxValue="99999999"/></div>
            <div header="市场价*" field="market_price"><input property="editor" class="mini-spinner" minValue="0"
                                                          maxValue="99999999"/></div>
            <div header="毛利率" field="maolilv"></div>
            <div header="采购价*" field="purchase_price"><input property="editor" class="mini-spinner" minValue="0"
                                                            maxValue="99999999"/></div>
            <div header="实际采购价*" field="r_purchase_price"><input property="editor" class="mini-spinner" minValue="0"
                                                                maxValue="99999999"/></div>
            <div header="分期数*" field="by_stages"><input property="editor" class="mini-spinner" minValue="0"
                                                       maxValue="99999999"/></div>
            <div header="费率（%）*" field="by_stages_rate"><input property="editor" class="mini-spinner" minValue="0"
                                                              maxValue="100"/></div>
            <div header="预估运费*" field="freight"><input property="editor" class="mini-spinner" minValue="0"
                                                      maxValue="99999999"/></div>
            <div header="赠品价格*" field="gift_price"><input property="editor" class="mini-spinner" minValue="0"
                                                         maxValue="99999999"/></div>
            <div header="银行佣金*" field="bank_commission"><input property="editor" class="mini-spinner" minValue="0"
                                                              maxValue="99999999"/></div>
            <div header="增值税等17%" field="zzs"></div>
            <div header="包装费*" field="packing_charge"><input property="editor" class="mini-spinner" minValue="0"
                                                            maxValue="99999999"/></div>
            <div header="返利金额*" field="rebate"><input property="editor" class="mini-spinner" minValue="0"
                                                     maxValue="99999999"/></div>
            <div header="天猫" field="tamll"><input property="editor" class="mini-textbox" style="width:100%;"/></div>
            <div header="天猫销售数量" field="tamll_sale"><input property="editor" class="mini-textbox" style="width:100%;"/>
            </div>
            <div header="京东" field="jd"><input property="editor" class="mini-textbox" style="width:100%;"/></div>
            <div header="京东销售数量" field="jd_sale"><input property="editor" class="mini-textbox" style="width:100%;"/>
            </div>
            <div header="广发" field="guangfa"><input property="editor" class="mini-textbox" style="width:100%;"/></div>
            <div header="建行" field="jianhang"><input property="editor" class="mini-textbox" style="width:100%;"/></div>
            <div header="招行" field="zhaohang"><input property="editor" class="mini-textbox" style="width:100%;"/></div>
            <div header="交行" field="jiaohang"><input property="editor" class="mini-textbox" style="width:100%;"/></div>
            <div header="民生" field="minsheng"><input property="editor" class="mini-textbox" style="width:100%;"/></div>
            <div header="平安" field="pingan"><input property="editor" class="mini-textbox" style="width:100%;"/></div>
            <div header="中信" field="zhongxin"><input property="editor" class="mini-textbox" style="width:100%;"/></div>
            <div header="产品毛重(kg)*" field="weight"><input property="editor" class="mini-textbox" style="width:100%;"/>
            </div>
            <div header="外包装尺寸*" field="packing_size"><input property="editor" class="mini-textbox" style="width:100%;"/>
            </div>
            <div header="推荐理由*" field="recommended_reason"><input property="editor" class="mini-textbox"
                                                                 style="width:100%;"/></div>
            <div header="赠品说明*" field="gift_note"><input property="editor" class="mini-textbox" style="width:100%;"/>
            </div>
            <div name="action" width="100" headerAlign="center">#</div>


        </div>
    </div>
    <br/>
    <?php if($showbtn==1){?>

    <?php if($access==true){?>
    <a class="mini-button" enabled="" id='query-bt' onclick="openaddauth()">&nbsp;&nbsp;添加授权&nbsp;&nbsp;</a>
    <a class="mini-button" enabled="" id='query-bt' onclick="selectitems()">&nbsp;&nbsp;添加物料&nbsp;&nbsp;</a>

    <div id='editForm1'>
        <br/>
        备注:<input style="width:400px;" name="remarkes" id='remarkes' class="mini-textarea" required="false"/>
        <br/><br/>
        <?php if($operatorType==0){?>
        <a class="mini-button" onclick="review(0)" id='query-bt'
           style="background-color: #81de81;color: white;width: 100px;height: 30px;line-height: 30px;font-weight: bold;">&nbsp;&nbsp;申请&nbsp;&nbsp;</a>
        <?php }?>
        <?php if($operatorType==1) {?>
        <a class="mini-button" onclick="review(1)" id='query-bt'
           style="background-color: #81de81;color: white;width: 100px;height: 30px;line-height: 30px;font-weight: bold;">&nbsp;&nbsp;通过&nbsp;&nbsp;</a>
        <a class="mini-button" onclick="review(-1)" id='query-bt'
           style="background-color: #de4c2b;color: white;width: 100px;height: 30px;line-height: 30px;font-weight: bold;">&nbsp;&nbsp;不通过&nbsp;&nbsp;</a>
        <?php }?>
    </div>

    <?php }?>

    <?php }?>

    <span>操作记录</span>
    <div id='acc_log'>
        <ul id='acc-log-ul'></ul>
    </div>
    </body>
    <script type="text/javascript">
        mini.parse();
        var grid = mini.get("datagrid1");
        var grid2 = mini.get("grid2");
        grid.on("drawcell", function (e) {
            var record = e.record,
                column = e.column,
                field = e.field,
                value = e.value;
            //格式化日期
            if (field == "U_FroTime" || field == 'U_ToTime') {
                if (mini.isDate(value)) e.cellHtml = mini.formatDate(value, "yyyy-MM-dd HH:mm:ss");
            }
            //action列，超连接操作按钮
            if (column.name == "action") {
                e.cellStyle = "text-align:center";
                e.cellHtml = '<a href="javascript:void(0)" onclick="deleteauth(' + record.id + ')"/>删除</a>&nbsp; ' + '<a href="/auth/save?id=' + record.id + '"/>编辑</a>&nbsp; ' + '<a href="/auth/down?id=' + record.id + '"/>下载附件</a>&nbsp; ';
            }
            if (field == "examine_status") {
                e.cellHtml = value == 1 ? "通过" : (value == 0 ? "待审核" : '未通过');
            }

            if (record.examine_status == 0)
                e.cellStyle = "background:#f9a8a8";
        });


        grid2.on("drawcell", function (e) {
            var record = e.record,
                column = e.column,
                field = e.field,
                value = e.value;
            //格式化日期
            if (field == "ctime") {
                if (mini.isDate(value)) e.cellHtml = mini.formatDate(value, "yyyy-MM-dd HH:mm:ss");

            }
            if (record.examine_status == 0)
                e.cellStyle = "background:#f9a8a8";

            if (field == "item_name") {
                var url = "/SapSupplier/itemedit/id/";
                e.cellHtml = '<a href="' + url + record.item_id + '"  target="_blank"  >' + value + '</a>';
            }

            if (field == "examine_status") {
                e.cellHtml = value == 1 ? "通过" : (value == 0 ? "待审核" : '未通过');
            }

            if (field == "logo") {
                e.cellHtml = '<img src="' + value + '"  style="width:50px" >';
            }

            if (field == "maoli") {
                var zzs = (fmt(record.mall_price) - fmt(record.purchase_price)) * 0.07;
                e.cellHtml = (fmt(record.mall_price) - fmt(record.purchase_price) + fmt(record.rebate) - fmt(record.freight) - fmt(record.gift_price) - fmt(record.bank_commission) - zzs).toFixed(2);
            }

            if (field == "maolilv") {
                var zzs = (fmt(record.mall_price) - fmt(record.purchase_price)) * 0.07;
                var maoli = (fmt(record.mall_price) - fmt(record.purchase_price) + fmt(record.rebate) - fmt(record.freight) - fmt(record.gift_price) - fmt(record.bank_commission) - zzs);

                e.cellHtml = (maoli / fmt(record.mall_price) * 100).toFixed(2);

            }

            if (field == "zzs") {
                var zzs = (fmt(record.mall_price) - fmt(record.purchase_price)) * 0.07;
                e.cellHtml = (zzs).toFixed(2);

            }

            if (field == "img") {

                e.cellHtml = "<img src='" + record.img + "' class='item-img'>";
            }
            if (column.name == "action") {
                e.cellStyle = "text-align:center";
                e.cellHtml = '<a href="javascript:void(0)" onclick="deleteprofit(' + record.id + ')"/>删除</a>&nbsp; ';
            }


        });


        //供应商发生改变
        function valuechanged(id) {
            grid.load({id: id});
            grid2.load({id: id});
            getlog(id);
        }

        function getlog(id) {
            $.ajax({
                url: '/SapSupplier/getlog',
                data: {id: id},
                dataType: 'json',
                type: 'post',
                success: function (data) {
                    var loghtml = "";
                    for (var i = 0; i < data.length; i++) {
                        loghtml += "<li>用户:" + data[i].user + ' 审核动作:' + data[i].info + " 时间：" + data[i].ctime + "</li>";
                    }
                    $("#acc-log-ul").html(loghtml);
                }

            })
        }

        function onButtonEdit2(e) {
            var btnEdit = this;
            mini.open({
                url: "/index.php?m=&c=SapSupplier&a=supplierselect",
                title: "选择供应商",
                width: 650,
                height: 380,
                ondestroy: function (action) {
                    if (action == "ok") {
                        var iframe = this.getIFrameEl();
                        var data = iframe.contentWindow.GetData();
                        data = mini.clone(data);    //必须
                        if (data) {
                            btnEdit.setValue(data.id);
                            btnEdit.setText(data.name);
                            valuechanged(data.id);
                        }
                    }

                }
            });

        }

        function selectitems() {
            var ck = check();
            if (ck != true) {
                mini.alert(ck);
                return;
            }
            ;
            mini.open({
                url: "/index.php?m=&c=SapSupplier&a=itemsselect",
                title: "选择物料",
                width: 650,
                height: 380,
                ondestroy: function (action) {
                    if (action == "ok") {
                        var iframe = this.getIFrameEl();
                        var data = iframe.contentWindow.GetData();
                        data = mini.clone(data);    //必须
                        if (data) {
                            if (checkItems(data.item_id)) {
                                mini.alert(data.item_name + '已存在列表中');
                                return;
                            }
                            var ndata = [];
                            ndata.item_name = data.item_name;
                            ndata.sap_code = data.sap_code;
                            ndata.logo = data.logo;
                            ndata.color = data.color;
                            ndata.weight = data.weight;
                            grid2.addRow(data);
                        }
                    }

                }
            });
        }

        //检查物料是否已存在列表中
        function checkItems(id) {
            var data = grid2.getData();
            for (var i = 0; i < data.length; i++) {
                if (data[i].item_id == id)
                    return true;
            }
            return false;
        }


        //添加授权
        function openaddauth() {
            var ck = check();
            if (ck != true) {
                mini.alert(ck);
                return;
            }
            ;
            var splid = mini.get('spl-select').getValue();
            var splname = mini.get('spl-select').getText();
            var prarms = "&splid=" + splid + "&splname=" + splname;
            mini.open({
                url: "/index.php?m=&c=auth&a=save" + prarms,
                title: "添加授权",
                width: 650,
                height: 380,
                ondestroy: function (action) {
                    if (action == "ok") {
                        var splid = mini.get('spl-select').getValue();
                        valuechanged(splid);
                    }

                }
            });
        }


        function deleteauth(id) {
            mini.confirm("确定删除该授权？", "确定？",
                function (action) {
                    if (action == "ok") {
                        dodeleteauth(id);
                    }
                }
            );

        }


        function dodeleteauth(authid) {

            $.ajax({
                url: '/SapSupplier/deleteauth',
                data: {authid: authid},
                dataType: 'json',
                type: 'post',
                success: function (data) {
                    if (data.code != 0) {
                        mini.alert(data.msg);
                        return;
                    }
                    grid.load({id: splid});
                }

            })
        }


        function deleteprofit(id) {
            mini.confirm("确定删除数据？", "确定？",
                function (action) {
                    if (action == "ok") {
                        dodeleteprofit(id);
                    }
                }
            );
        }


        function dodeleteprofit(pid) {

            $.ajax({
                url: '/SapSupplier/deleteprofit',
                data: {id: pid},
                dataType: 'json',
                type: 'post',
                success: function (data) {
                    if (data.code != 0) {
                        mini.alert(data.msg);
                        return;
                    }
                    grid2.load({id: mini.get('spl-select').getValue()});
                }


            })
        }

        function check() {

            var splid = mini.get('spl-select').getValue();
            if (splid == '' || splid == null) {

                return '供应商错误,请选择供应商';
            }

            return true;
        }


        function cellendedit(e) {
            column = e.column,
                field = e.field,
                value = e.value;
            if (e.record._state != null) {
                e.record.sid = mini.get('spl-select').getValue();

                update(mini.encode(e.record));
            }
        }


        function update(data) {

            $.ajax({
                url: "/SapSupplier/updatesupplierprofit",
                type: "post",
                data: {
                    data: data
                },
                dataType: 'json',
                success: function (data) {
                    var id = mini.get('spl-select').getValue();
                    grid2.load({id: id});
                }
            });
        }

        function fmt(num) {
            return parseFloat(num);
        }

        function review(status) {
            var ck = check();
            if (ck != true) {
                mini.alert(ck);
                return;
            }
            ;

            mini.confirm("确定要执行该操作吗？", "确定?",
                function (action) {
                    if (action == "ok") {
                        doreview(status);
                    }
                });

        }

        function doreview(status) {

            mini.mask({
                el: document.body,
                cls: 'mini-mask-loading',
                html: '保存中...'
            });

            var remarkes = mini.get('remarkes').getValue();
            $.ajax({
                url: "/SapSupplier/review",
                data: {id: mini.get('spl-select').getValue(), status: status, remarkes: remarkes},
                type: "post",
                success: function (data) {
                    if (data == 1) {
                        mini.unmask(document.body);
                        mini.alert('保存成功!');
                        valuechanged(mini.get('spl-select').getValue());
                        return;
                    } else {
                        mini.unmask(document.body);
                        mini.alert(data.msg);
                    }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert(jqXHR.responseText);
                }
            });
        }

        valuechanged(<?php echo $supplierID;?>);

    </script>


    </html>
</block>
